from app.models.po.gaolu.gaolu_diagnose_rule import GaoLuDiagnoseRulePO
from typing import List
import json


def cal(rules: List[GaoLuDiagnoseRulePO], result_dict):
    rule_dict = {}
    for rule in rules:
        section = json.loads(rule.section)
        rule_dict[rule.name] = dict(section=section, scope=rule.scope, total_score=rule.score)

    score_dict = {}
    for k, v in result_dict.items():
        config = rule_dict[k]
        for section_key, inner_section in config['section'].items():
            if "i" not in section_key: continue
            # inner_section = item['section']
            matched = match_section(v, inner_section)
            if matched:
                index = section_key.split("i")[1]
                score_rate = config['section'].get("s" + str(index))
                score = config['total_score'] * score_rate
                score_dict[k] = dict(score=score, scope=config['scope'], key=k)
                break

    return score_dict


def match_section(value, section):
    if section is None: return None
    section  = section.strip()
    if "≥" in section:
        num = float(section.split("≥")[1])
        return value >= num
    if ">" in section:
        num = float(section.split(">")[1])
        return value > num
    if "<" in section:
        num = float(section.split("<")[1])
        return value < num
    if "≤" in section:
        num = float(section.split("≤")[1])
        return value <= num
    if "[" in section or ")" in section or "]" in section or "(" in section:
        left_open = True if section.startswith("[") else False
        right_open = True if section.endswith("]") else False

        inner = section.replace("[", "").replace(")", "").replace("(", "").replace("]", "")
        left = float(inner.split(",")[0])
        right = float(inner.split(",")[1])

        if left_open and not right_open:
            return value >= left and value < right

        if left_open and right_open:
            return value >= left and value <= right

        if not left_open and right_open:
            return value > left and value <= right

        if not left_open and not right_open:
            return value > left and value < right


rules = [
    {
        "lower": 30.0,
        "desc": "日料批",
        "scope": "jj",
        "scope_name": "技经指标",
        "frequency": "天",
        "score": 5,
        "upper": 999999.0,
        "unit": "t",
        "id": 1,
        "rule_group_id": 1,
        "section": "{\"i0\": \"<30\", \"i1\": \"[30,58)\", \"i2\": \"[58,86)\", \"i3\": \"[86,115)\", \"i4\": \"[115,141)\", \"i5\": \"≥141\", \"i6\": null, \"s0\": 0, \"s1\": 0.2, \"s2\": 0.4, \"s3\": 0.6, \"s4\": 0.8, \"s5\": 1, \"s6\": null}",
        "name": "CG_LT_GL_GL04_Rliaopi",
        "type": "二级"
    },
    {
        "lower": -999999.0,
        "desc": "燃料比",
        "scope": "jj",
        "scope_name": "技经指标",
        "frequency": "天",
        "score": 7,
        "upper": 570.0,
        "unit": "kg/t",
        "id": 2,
        "rule_group_id": 1,
        "section": "{\"i0\": \"≤530\", \"i1\": \"(530,540] \", \"i2\": \"(540,550]\", \"i3\": \"(550,560] \", \"i4\": \"(560,570] \", \"i5\": \">570\", \"i6\": null, \"s0\": 1, \"s1\": 0.8, \"s2\": 0.6, \"s3\": 0.4, \"s4\": 0.2, \"s5\": 0, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RRLB",
        "type": "一级"
    },
    {
        "lower": 120.0,
        "desc": "煤比",
        "scope": "jj",
        "scope_name": "技经指标",
        "frequency": "天",
        "score": 5,
        "upper": 999999.0,
        "unit": "kg/t",
        "id": 3,
        "rule_group_id": 1,
        "section": "{\"i0\": \"<120\", \"i1\": \"[120,125) \", \"i2\": \"[125,130)\", \"i3\": \"[130,135)\", \"i4\": \"[135,140)\", \"i5\": \"≥140\", \"i6\": null, \"s0\": 0, \"s1\": 0.2, \"s2\": 0.4, \"s3\": 0.6, \"s4\": 0.8, \"s5\": 1, \"s6\": null}",
        "name": "CG_LT_GL_GL04_Rcokerate",
        "type": "二级"
    },
    {
        "lower": 4600.0,
        "desc": "风量",
        "scope": "sf",
        "scope_name": "送风参数",
        "frequency": "天",
        "score": 3,
        "upper": 999999.0,
        "unit": "m3/min",
        "id": 4,
        "rule_group_id": 1,
        "section": "{\"i0\": \"<4600\", \"i1\": \"[4600,4800) \", \"i2\": \"[4800,4900)\", \"i3\": \"[4900,5000)\", \"i4\": \"[5000,5100)\", \"i5\": \"≥5100\", \"i6\": null, \"s0\": 0, \"s1\": 0.2, \"s2\": 0.4, \"s3\": 0.6, \"s4\": 0.8, \"s5\": 1, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RLFLL",
        "type": "二级"
    },
    {
        "lower": 5000.0,
        "desc": "富氧流量",
        "scope": "sf",
        "scope_name": "送风参数",
        "frequency": "天",
        "score": 3,
        "upper": 999999.0,
        "unit": "m3/h",
        "id": 5,
        "rule_group_id": 1,
        "section": "{\"i0\": \"<5000\", \"i1\": \"[5000,7000) \", \"i2\": \"[7000,9000)\", \"i3\": \"[9000,10000)\", \"i4\": \"[10000,11000)\", \"i5\": \"≥11000\", \"i6\": null, \"s0\": 0, \"s1\": 0.2, \"s2\": 0.4, \"s3\": 0.6, \"s4\": 0.8, \"s5\": 1, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RFYLL",
        "type": "三级"
    },
    {
        "lower": -999999.0,
        "desc": "日减风次数",
        "scope": "sf",
        "scope_name": "送风参数",
        "frequency": "天",
        "score": 7,
        "upper": 3.0,
        "unit": "次",
        "id": 6,
        "rule_group_id": 1,
        "section": "{\"i0\": \"≤0\", \"i1\": \"(0,1] \", \"i2\": \"(1,2] \", \"i3\": \"(2,3] \", \"i4\": \">3\", \"i5\": null, \"i6\": null, \"s0\": 1, \"s1\": 0.8, \"s2\": 0.6, \"s3\": 0.2, \"s4\": 0, \"s5\": null, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RjianfengCi",
        "type": "一级"
    },
    {
        "lower": 29.0,
        "desc": "透气性指数",
        "scope": "sf",
        "scope_name": "送风参数",
        "frequency": "天",
        "score": 5,
        "upper": 999999.0,
        "unit": "%",
        "id": 7,
        "rule_group_id": 1,
        "section": "{\"i0\": \"<29\", \"i1\": \"[29,29.5) \", \"i2\": \"[29.5,30)\", \"i3\": \"[30,30.5)\", \"i4\": \"[30.5,31)\", \"i5\": \"≥31\", \"i6\": null, \"s0\": 0, \"s1\": 0.2, \"s2\": 0.4, \"s3\": 0.6, \"s4\": 0.8, \"s5\": 1, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RTQXZS",
        "type": "二级"
    },
    {
        "lower": 14.0,
        "desc": "上部压差占比",
        "scope": "sf",
        "scope_name": "送风参数",
        "frequency": "天",
        "score": 3,
        "upper": 26.0,
        "unit": "%",
        "id": 8,
        "rule_group_id": 1,
        "section": "{\"i0\": \"≤14\", \"i1\": \"(14,17] \", \"i2\": \"(17,20]\", \"i3\": \"(20,23]\", \"i4\": \"(23,26]\", \"i5\": \">26\", \"i6\": null, \"s0\": 0.6, \"s1\": 0.8, \"s2\": 1, \"s3\": 0.6, \"s4\": 0.4, \"s5\": 0.2, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RSBYCZB",
        "type": "三级"
    },
    {
        "lower": 63.0,
        "desc": "下部压差占比",
        "scope": "sf",
        "scope_name": "送风参数",
        "frequency": "天",
        "score": 3,
        "upper": 570.0,
        "unit": "%",
        "id": 9,
        "rule_group_id": 1,
        "section": "{\"i0\": \"≤63\", \"i1\": \"(63,65] \", \"i2\": \"(65,66]\", \"i3\": \"(66,68]\", \"i4\": \"(68,70]\", \"i5\": \"(70,72]\", \"i6\": \">570\", \"s0\": 0, \"s1\": 0.2, \"s2\": 0.4, \"s3\": 0.6, \"s4\": 0.8, \"s5\": 1, \"s6\": 0.6}",
        "name": "CG_LT_GL_GL04_RXBYCZB",
        "type": "三级"
    },
    {
        "lower": 2200.0,
        "desc": "理论燃烧温度",
        "scope": "sf",
        "scope_name": "送风参数",
        "frequency": "天",
        "score": 3,
        "upper": 2550.0,
        "unit": "℃",
        "id": 10,
        "rule_group_id": 1,
        "section": "{\"i0\": \"<2200\", \"i1\": \"[2200,2300) \", \"i2\": \"[2300,2400)\", \"i3\": \"[2400,2500)\", \"i4\": \"[2500,2550)\", \"i5\": \"≥2550\", \"i6\": null, \"s0\": 0, \"s1\": 0.2, \"s2\": 0.6, \"s3\": 1, \"s4\": 0.6, \"s5\": 0, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RLLRSWD",
        "type": "三级"
    },
    {
        "lower": 10000.0,
        "desc": "鼓风动能",
        "scope": "sf",
        "scope_name": "送风参数",
        "frequency": "天",
        "score": 3,
        "upper": 999999.0,
        "unit": "kg·m/s",
        "id": 11,
        "rule_group_id": 1,
        "section": "{\"i0\": \"<10000\", \"i1\": \"[10000,10500)\", \"i2\": \"[10500,11000)\", \"i3\": \"[11000,11500)\", \"i4\": \"[11500,12000)\", \"i5\": \"≥12000\", \"i6\": null, \"s0\": 0, \"s1\": 0.2, \"s2\": 0.4, \"s3\": 0.6, \"s4\": 0.8, \"s5\": 1, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RGFDNKG",
        "type": "三级"
    },
    {
        "lower": -999999.0,
        "desc": "炉顶温度",
        "scope": "mql",
        "scope_name": "煤气流监控",
        "frequency": "天",
        "score": 3,
        "upper": 210.0,
        "unit": "℃",
        "id": 12,
        "rule_group_id": 1,
        "section": "{\"i0\": \"≤150\", \"i1\": \"(150,170]\", \"i2\": \"(170,190]\", \"i3\": \"(190,200]\", \"i4\": \"(200,210]\", \"i5\": \">210\", \"i6\": null, \"s0\": 1, \"s1\": 0.8, \"s2\": 0.6, \"s3\": 0.4, \"s4\": 0.2, \"s5\": 0, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RLDWD",
        "type": "三级"
    },
    {
        "lower": -999999.0,
        "desc": "热负荷",
        "scope": "mql",
        "scope_name": "煤气流监控",
        "frequency": "天",
        "score": 3,
        "upper": 23000.0,
        "unit": None,
        "id": 13,
        "rule_group_id": 1,
        "section": "{\"i0\": \"≤15000\", \"i1\": \"(15000,17000] \", \"i2\": \"(17000,19000]  \", \"i3\": \"(19000,21000] \", \"i4\": \"(21000,23000] \", \"i5\": \">23000\", \"i6\": null, \"s0\": 1, \"s1\": 0.8, \"s2\": 0.6, \"s3\": 0.4, \"s4\": 0.2, \"s5\": 0, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RRFH",
        "type": "三级"
    },
    {
        "lower": 50.0,
        "desc": "阀座温度",
        "scope": "mql",
        "scope_name": "煤气流监控",
        "frequency": "天",
        "score": 5,
        "upper": 999999.0,
        "unit": "℃",
        "id": 14,
        "rule_group_id": 1,
        "section": "{\"i0\": \"＜50\", \"i1\": \"[50,60)\", \"i2\": \"[60,70)\", \"i3\": \"[70,80)\", \"i4\": \"[80,90)\", \"i5\": \"≥90\", \"i6\": null, \"s0\": 0, \"s1\": 0.2, \"s2\": 0.4, \"s3\": 0.6, \"s4\": 0.8, \"s5\": 1, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RFZWD",
        "type": "二级"
    },
    {
        "lower": -999999.0,
        "desc": "崩、坐料管道次数",
        "scope": "zt",
        "scope_name": "渣铁处理",
        "frequency": "天",
        "score": 7,
        "upper": 3.0,
        "unit": "次",
        "id": 15,
        "rule_group_id": 1,
        "section": "{\"i0\": \"＜1\", \"i1\": \"[1,2)\", \"i2\": \"[2,3)\", \"i3\": \"≥3\", \"i4\": null, \"i5\": null, \"i6\": null, \"s0\": 1, \"s1\": 0.6, \"s2\": 0.2, \"s3\": 0, \"s4\": null, \"s5\": null, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RbengzuoguanCi",
        "type": "一级"
    },
    {
        "lower": 10.0,
        "desc": "日出铁次数",
        "scope": "zt",
        "scope_name": "渣铁处理",
        "frequency": "天",
        "score": 3,
        "upper": 999999.0,
        "unit": "次",
        "id": 16,
        "rule_group_id": 1,
        "section": "{\"i0\": \"＜10\", \"i1\": \"[10,12)\", \"i2\": \"[12,13)\", \"i3\": \"[13,14)\", \"i4\": \"≥14\", \"i5\": null, \"i6\": null, \"s0\": 0, \"s1\": 0.4, \"s2\": 0.6, \"s3\": 0.8, \"s4\": 1, \"s5\": null, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RchutieCi",
        "type": "三级"
    },
    {
        "lower": 1440.0,
        "desc": "PT",
        "scope": "tslz",
        "scope_name": "铁水炉渣",
        "frequency": "天",
        "score": 5,
        "upper": 1500.0,
        "unit": "℃",
        "id": 17,
        "rule_group_id": 1,
        "section": "{\"i0\": \"＜1440\", \"i1\": \"[1440,1450)\", \"i2\": \"[1450,1460)\", \"i3\": \"[1460,1480]\", \"i4\": \"(1480,1490]\", \"i5\": \"(1490,1500]\", \"i6\": \"＞1500\", \"s0\": 0, \"s1\": 0.2, \"s2\": 0.6, \"s3\": 1, \"s4\": 0.6, \"s5\": 0.2, \"s6\": 0}",
        "name": "CG_LT_GL_GL04_Rtswd",
        "type": "二级"
    },
    {
        "lower": 0.1,
        "desc": "[Si+Ti]",
        "scope": "tslz",
        "scope_name": "铁水炉渣",
        "frequency": "天",
        "score": 3,
        "upper": 0.55,
        "unit": "%",
        "id": 18,
        "rule_group_id": 1,
        "section": "{\"i0\": \"＜0.1\", \"i1\": \"[0.1,0.15)\", \"i2\": \"[0.15,0.2)\", \"i3\": \"[0.2,0.4]\", \"i4\": \"(0.5,0.45]\", \"i5\": \"（0.5,0.55]\", \"i6\": \"＞0.55\", \"s0\": 0, \"s1\": 0.2, \"s2\": 0.6, \"s3\": 1, \"s4\": 0.6, \"s5\": 0.2, \"s6\": 0}",
        "name": "CG_LT_GL_GL04_RSi_Ti",
        "type": "三级"
    },
    {
        "lower": 0.55,
        "desc": "MgO/Al2O3",
        "scope": "tslz",
        "scope_name": "铁水炉渣",
        "frequency": "天",
        "score": 3,
        "upper": 999999.0,
        "unit": None,
        "id": 19,
        "rule_group_id": 1,
        "section": "{\"i0\": \"≤0.55\", \"i1\": \"(0.55,0.6]\", \"i2\": \"(0.6,0.63]\", \"i3\": \"(0.63,0.65]\", \"i4\": \"(0.65,0.7]\", \"i5\": \">0.7\", \"i6\": null, \"s0\": 0, \"s1\": 0.2, \"s2\": 0.8, \"s3\": 1, \"s4\": 0.6, \"s5\": 0.4, \"s6\": null}",
        "name": "CG_LT_GL_GL04_RMg_Al",
        "type": "三级"
    },
    {
        "lower": -999999.0,
        "desc": "34m",
        "scope": "ltlg",
        "scope_name": "炉体炉缸监控",
        "frequency": "天",
        "score": 3,
        "upper": 100.0,
        "unit": "℃",
        "id": 20,
        "rule_group_id": 1,
        "section": "{\"i0\": \"≤80\", \"i1\": \"(80,85]\", \"i2\": \"(85,90]\", \"i3\": \"(90,95]\", \"i4\": \"(95,100]\", \"i5\": \"＞100\", \"i6\": null, \"s0\": 1, \"s1\": 0.8, \"s2\": 0.6, \"s3\": 0.4, \"s4\": 0.2, \"s5\": 0, \"s6\": null}",
        "name": "CG_LT_GL_GL04_R34m",
        "type": "三级"
    },
    {
        "lower": -999999.0,
        "desc": "26m",
        "scope": "ltlg",
        "scope_name": "炉体炉缸监控",
        "frequency": "天",
        "score": 3,
        "upper": 85.0,
        "unit": "℃",
        "id": 21,
        "rule_group_id": 1,
        "section": "{\"i0\": \"≤65\", \"i1\": \"(65,70]\", \"i2\": \"(70,75]\", \"i3\": \"(75,80]\", \"i4\": \"(80,85]\", \"i5\": \"＞85\", \"i6\": null, \"s0\": 1, \"s1\": 0.8, \"s2\": 0.6, \"s3\": 0.4, \"s4\": 0.2, \"s5\": 0, \"s6\": null}",
        "name": "CG_LT_GL_GL04_R26m",
        "type": "三级"
    },
    {
        "lower": -999999.0,
        "desc": "24m",
        "scope": "ltlg",
        "scope_name": "炉体炉缸监控",
        "frequency": "天",
        "score": 5,
        "upper": 110.0,
        "unit": "℃",
        "id": 22,
        "rule_group_id": 1,
        "section": "{\"i0\": \"≤70\", \"i1\": \"(70,80]\", \"i2\": \"(80,90]\", \"i3\": \"(90,100]\", \"i4\": \"(100,110]\", \"i5\": \"＞110\", \"i6\": null, \"s0\": 1, \"s1\": 0.8, \"s2\": 0.6, \"s3\": 0.4, \"s4\": 0.2, \"s5\": 0, \"s6\": null}",
        "name": "CG_LT_GL_GL04_R24m",
        "type": "二级"
    },
    {
        "lower": 50.0,
        "desc": "17m",
        "scope": "ltlg",
        "scope_name": "炉体炉缸监控",
        "frequency": "天",
        "score": 3,
        "upper": 65.0,
        "unit": "℃",
        "id": 23,
        "rule_group_id": 1,
        "section": "{\"i0\": \"＜50\", \"i1\": \"[50,65]\", \"i2\": \"＞65\", \"i3\": null, \"i4\": null, \"i5\": null, \"i6\": null, \"s0\": 0, \"s1\": 1, \"s2\": 0, \"s3\": null, \"s4\": null, \"s5\": null, \"s6\": null}",
        "name": "CG_LT_GL_GL04_R17m",
        "type": "三级"
    },
    {
        "lower": -999999.0,
        "desc": "炉芯温度",
        "scope": "ltlg",
        "scope_name": "炉体炉缸监控",
        "frequency": "天",
        "score": 3,
        "upper": 470.0,
        "unit": "℃",
        "id": 24,
        "rule_group_id": 1,
        "section": "{\"i0\": \"[350,380)\", \"i1\": \"[380,410)\", \"i2\": \"[410,420)\", \"i3\": \"[420,440)\", \"i4\": \"[440,470)\", \"i5\": \"≥470\", \"i6\": null, \"s0\": 0.2, \"s1\": 0.4, \"s2\": 0.6, \"s3\": 1, \"s4\": 0.8, \"s5\": 0, \"s6\": null}",
        "name": "CG_LT_GL_GL04_Rluxin2",
        "type": "三级"
    }
]
if __name__ == "__main__":
    rule_list = []
    for rule in rules:
        po = GaoLuDiagnoseRulePO()
        po.__dict__.update(rule)
        rule_list.append(po)

    score_dict = cal(rule_list, {'CG_LT_GL_GL04_Rliaopi': 142.0, 'CG_LT_GL_GL04_RRLB': 509.0, 'CG_LT_GL_GL04_Rcokerate': 125.0,
                    'CG_LT_GL_GL04_RLFLL': 5240.23243599592, 'CG_LT_GL_GL04_RFYLL': 8294.860391101076,
                    'CG_LT_GL_GL04_RTQXZS': 29.236928948481943, 'CG_LT_GL_GL04_RSBYCZB': 17.31907627477264,
                    'CG_LT_GL_GL04_RXBYCZB': 69.28775598114304, 'CG_LT_GL_GL04_RLLRSWD': 2363.62254991947,
                    'CG_LT_GL_GL04_RGFDNKG': 14057.477533906202, 'CG_LT_GL_GL04_RLDWD': 146.30545056382832,
                    'CG_LT_GL_GL04_RRFH': 2469.8061712187837, 'CG_LT_GL_GL04_RFZWD': 92.22699496334027,
                    'CG_LT_GL_GL04_RchutieCi': 13, 'CG_LT_GL_GL04_Rtswd': 1466.0, 'CG_LT_GL_GL04_RSi_Ti': 0.2657,
                    'CG_LT_GL_GL04_RMg_Al': 0.8294035024804255, 'CG_LT_GL_GL04_R34m': 88.66219712484732,
                    'CG_LT_GL_GL04_R26m': 51.69687477088196, 'CG_LT_GL_GL04_R24m': 51.640324058572084,
                    'CG_LT_GL_GL04_R17m': 50.95800309314078, 'CG_LT_GL_GL04_Rluxin2': 479.0186630173109,
                    'CG_LT_GL_GL04_RbengzuoguanCi': 1, 'CG_LT_GL_GL04_RjianfengCi': 2}
        )

    item_group = {}
    zf = 0  # 总分
    for key, item in score_dict.items():
        score = item_group.get(item['scope'], 0)
        score = score + item['score']
        item_group[item['scope']] = score
        item_group[item['key']] = item['score']
        zf = zf + item['score']

    pass
